package com.samba.entites;

import java.beans.Transient;
import java.io.Serializable;
import java.util.Date;


import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
@NamedQueries( {
    @NamedQuery(name = "findAllCreditProjet", query = "SELECT c FROM CreditProjetUtilisateur c"),
    @NamedQuery(name = "findAllProjetsByUserId", query = "SELECT c FROM CreditProjetUtilisateur c WHERE c.utlisateur.id = :userId"),
    @NamedQuery(name = "findCreditProjetUtilisateuryId", query = "SELECT c FROM CreditProjetUtilisateur c WHERE c.id = :id")
    }
)
@Table(name= "TABLE_CRED_PROJ_UTIL")
public class CreditProjetUtilisateur  implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	
	public enum EtatFinancementCredit {
		
		FINANCE_EN_ATTENTE, FINANCE,REMBOURSEMENT_EN_ATTENTE, REMBOURSE
	};
	
	private int id;
	private Date dateDeFinanacement;
	private Date dateDeRembousement;
	
	private int valeurDuPret;
	
	
	private EtatFinancementCredit etat;
	
	private Projet projet;
	
	
	private Utilisateur utlisateur;
	
	

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	public int getId() {
		return id;
	}
	
	public void setId(int id) {
		this.id = id;
	}

	public int getValeurDuPret() {
		return valeurDuPret;
	}

	public void setValeurDuPret(int valeurDuPret) {
		this.valeurDuPret = valeurDuPret;
	}

	@Temporal(TemporalType.DATE)
	public Date getDateDeFinanacement() {
		return dateDeFinanacement;
	}
	
	public void setDateDeFinanacement(Date dateDeFinanacement) {
		this.dateDeFinanacement = dateDeFinanacement;
	}
	
	@Temporal(TemporalType.DATE)
	public Date getDateDeRembousement() {
		return dateDeRembousement;
	}
	
	public void setDateDeRembousement(Date dateDeRembousement) {
		this.dateDeRembousement = dateDeRembousement;
	}
	
	@Enumerated(value = EnumType.STRING)
	//Par default EnymType.ORDINAL est utiliser par defaut
	public EtatFinancementCredit getEtat() {
		return etat;
	}
	
	public void setEtat(EtatFinancementCredit etat) {
		this.etat = etat;
	}
	
	
	@Transient
	public boolean est_En_Attente_Finanacement()
	{
		return EtatFinancementCredit.FINANCE_EN_ATTENTE.equals(etat);
	}
	
	@Transient
	public boolean est_Financer()
	{
		return EtatFinancementCredit.FINANCE.equals(etat);
	}
	
	
	@Transient
	public boolean est_Remboureser()
	{
		return EtatFinancementCredit.REMBOURSE.equals(etat);
	}
	
	@Transient
	public boolean est_En_Attente_Remboursement()
	{
		Date now = new Date();
		try
		{
			return ((!EtatFinancementCredit.REMBOURSE.equals(etat)) && now.getTime() > getDateDeRembousement().getTime());
			
		}catch(Exception e)
		{
			return false;
		}
	}
	
	@ManyToOne
	@JoinColumn(name = "projet_fk")
	public Projet getProjet() {
		return projet;
	}

	public void setProjet(Projet projet) {
		this.projet = projet;
	}
	
	@ManyToOne
	@JoinColumn(name = "utilisateurFk")
	public Utilisateur getUtlisateur() {
		return utlisateur;
	}
	public void setUtlisateur(Utilisateur utlisateur) {
		this.utlisateur = utlisateur;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + id;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		CreditProjetUtilisateur other = (CreditProjetUtilisateur) obj;
		if (id != other.id)
			return false;
		return true;
	}
	@Override
	public String toString() {
		return "CreditProjetUtilisateur [id=" + id + "]";
	}
}
